package com.alarmclock_n18.contorl;

import com.alarmclock_n18.AlarmActivity;
import com.alarmclock_n18.MainActivity;
import com.alarmclock_n18.model.AlarmClock;
import com.alarmclock_n18.util.GetLogger;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.util.Log;
import android.widget.Toast;

public class AlarmClockRecevice extends BroadcastReceiver {
	public static final int ALARM_ID01 = 1;  
	public static final int ALARM_ID02 = 2;  
	public static final int ALARM_ID03 = 3;  
	public static final int ALARM_ID04 = 4;  
	public static final int ALARM_ID05 = 5;  
	
	public static final String ALARM_TAG = "alarm_id";
	/**
	 * <receiver android:name="AlarmReceiver">
     *  </receiver>
	 */
	public AlarmClockRecevice(){
		GetLogger.println("Receive:创建");
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		GetLogger.v("Receive:","onReceive:" +context.toString());
		if(context == null){
			GetLogger.println("context null");
		}else{
			  //锟截憋拷锟斤拷锟斤拷
		  
//		    Intent intent2 = new Intent();
//			intent.setAction(MainActivity.ALARM_TIME);
		    //TODO intent 锟斤拷值
		    int id = intent.getIntExtra(AlarmClock.ID, -1);
		    String str_img_id = intent.getStringExtra(AlarmClock.IMG_ID);
		    String str_music_id = intent.getStringExtra(AlarmClock.MUSIC_ID);
		    long  time = intent.getLongExtra(AlarmClock.TIME, -1);
		    int data = intent.getIntExtra(AlarmClock.DATA, 0);
		    GetLogger.println("onReceive Intetnt id:"+intent.getIntExtra("clock_id", -1));
		    GetLogger.println("onReceive Intetnt img_id:"+intent.getStringExtra("img_id"));
		    GetLogger.println("onReceive Intetnt music_id:"+intent.getStringExtra("music_id"));
		 
		    
		    handleClock(context,data,intent);
		    
		    
		    //StartActivity
		    Intent intent1 = new Intent(context,AlarmActivity.class);
		    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		    intent1.putExtra(AlarmClock.ID, id);
		    intent1.putExtra(AlarmClock.IMG_ID, str_img_id);
		    intent1.putExtra(AlarmClock.MUSIC_ID, str_music_id);
		    intent1.putExtra(AlarmClock.TIME, time);
		    context.startActivity(intent1);
		    
			Toast.makeText(context, "This the time", Toast.LENGTH_LONG).show();
		}
	    
		if(intent != null){
			GetLogger.println(""+intent.getAction());
		}
	    
	  
	    
	    //TODO 
	    abortBroadcast();//停止此次广播（只能停止有序广播）
	}

	//处理闹钟   获取当前日期    比对星期   重设闹钟或是取消闹钟
	private void handleClock(Context context,int weekday,Intent intent){
		  AlarmManager almm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		  PendingIntent pIntent = PendingIntent.getBroadcast(context,
		    		intent.getIntExtra("clock_id", -1), intent, PendingIntent.FLAG_UPDATE_CURRENT);
		    
	     if(((byte)(weekday & 127)== MainActivity.getNowDayOfWeek())||weekday == 127){
			  
	     }
//		  data & MainActivity.getNowDayOfWeek();
		  
		  
		  almm.cancel(pIntent);
	}
}
